<?php
namespace app\system\controller;

use app\common\model\UserModel;
use think\Controller;
use think\Request;

class PublicController extends Controller
{
    private $userModel;

    public function __construct(Request $request)
    {
        parent::__construct($request);
        $this->userModel = new UserModel();
    }

    /**
     * 系统用户登录
     * @return mixed
     */
    public function sysLogin()
    {
        return $this->fetch();
    }
    /**
     * 站点创始人登录
     * @return mixed
     */
    public function siteLogin()
    {
        return $this->fetch();
    }
    /**
     * 站点操作员登录
     * @return mixed
     */
    public function operatorLogin()
    {
        return $this->fetch();
    }
    /**
     * 用户登录
     */
    public function doLogin(){
        if ($this->request->isAjax()) {
            $data   = $this->request->param();
            $result = $this->validate($data, ['username' => 'require', 'password' => 'require', 'utype|会员类型' => 'require']);
            if (true !== $result) {
                $this->error($result);
            }
            $utype  = $data['utype'];
            $result = $this->userModel->checkLogin($data['username'], $data['password'], $utype);
            if ($result === false) {
                $this->error($this->userModel->getError());
            }
            $siteId = $this->userModel->where('uid',$result)->value('site_id');
            cookie('siteId', str_encode($siteId));
            cookie('utype', str_encode($utype));
            cookie($data['utype'] . 'Des', str_encode($result));
            $this->success('登录成功！', url('Index/index'));
        }else{
            $this->error('非法请求！');
        }
    }

}
